package com.irdstudio.efp.esb.api.service.esb;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.irdstudio.basic.framework.core.annotation.EsbRetCd;
import com.irdstudio.basic.framework.core.annotation.EsbRetMsg;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.common.constant.EsbSysHeadEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.stream.Stream;

@Service
public class EsbRespServcie<RESP> {

    private static Logger logger = LoggerFactory.getLogger(EsbRespServcie.class);

    public EsbRespServiceBean getSucessBean(EsbReqServiceBean esbReqServiceBean, RESP respBean,EsbRetCodeStandard esbRetCodeStandard){
        //根据注解设置body的响应码跟相应信息
        Stream.of(respBean.getClass().getDeclaredFields()).forEach((field -> {
            field.setAccessible(true);
            EsbRetCd esbRetCdAnnotation = field.getDeclaredAnnotation(EsbRetCd.class);
            if (!Objects.isNull(esbRetCdAnnotation)) {
                //输出码
                try {
                    field.set(respBean,esbRetCodeStandard.getValue());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            EsbRetMsg esbRetMsgAnnotation = field.getDeclaredAnnotation(EsbRetMsg.class);
            if (!Objects.isNull(esbRetMsgAnnotation)) {
                //输出信息
                try {
                    field.set(respBean,esbRetCodeStandard.getDesc());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                JSONObject.toJSONString(respBean, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteMapNullValue),
                EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(),
                esbRetCodeStandard.getValue(),
                esbRetCodeStandard.getDesc());
    }

    public EsbRespServiceBean getEsbRespServiceBeanByException(EsbReqServiceBean esbReqServiceBean, Exception exception, RESP respBean, EsbRetCodeStandard esbRetCodeStandard) {
        Stream.of(respBean.getClass().getDeclaredFields()).forEach((field -> {
            field.setAccessible(true);
            EsbRetCd esbRetCdAnnotation = field.getDeclaredAnnotation(EsbRetCd.class);
            if (!Objects.isNull(esbRetCdAnnotation)) {
                //输出码
                try {
                    field.set(respBean,esbRetCodeStandard.getValue());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            EsbRetMsg esbRetMsgAnnotation = field.getDeclaredAnnotation(EsbRetMsg.class);
            if (!Objects.isNull(esbRetMsgAnnotation)) {
                //输出信息
                try {
                    field.set(respBean,exception.getMessage());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }));
        return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
                JSONObject.toJSONString(respBean, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteMapNullValue),
                EsbSysHeadEnums.RetStEnum.FAILURE.getCode(),
                esbRetCodeStandard.getValue(),
                exception.getMessage());
    }

}
